//
//  PictureController.m
//  MyOpenGLESLearn_iOS
//
//  Created by Gaomingyang on 2021/4/14.
//

#import "PictureController.h"
#import "PictureRender.h"
#import "YCMenuView.h"

#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height

@interface PictureController ()
@property (nonatomic, strong) GLKView *glkView;
@property (nonatomic, strong) PictureRender *objRender;

@property (nonatomic,strong)YCMenuView   *mView;
@property (nonatomic,strong)UIButton *menuBtn;
@property (nonatomic,strong)UIBarButtonItem *rightBar2;
@property (nonatomic,strong)NSArray      *arr;

@property (nonatomic,assign)BOOL   isProgressWhole;//是否处理全部
@end

@implementation PictureController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self commonInit];
}

-(void)commonInit{
    self.title = @"纹理";
    self.view.backgroundColor = [UIColor whiteColor];
    self.isProgressWhole = YES;
    EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    //是指当前的context
    [EAGLContext setCurrentContext:context];
    //2.创建GLKView 并设置代理
    self.glkView = [[GLKView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.width) context:context];
    self.glkView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.glkView];
    self.objRender = [[PictureRender alloc] initWithGLKView:self.glkView];
    
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc] initWithCustomView:self.menuBtn];
    NSString *barTitle = self.isProgressWhole ? @"处理全部":@"处理一半";
    self.rightBar2 = [[UIBarButtonItem alloc] initWithTitle:barTitle style:UIBarButtonItemStylePlain target:self action:@selector(rightBar2Click:)];
    
//    self.navigationItem.rightBarButtonItems = @[rightBar,self.rightBar2];
    self.navigationItem.rightBarButtonItem = rightBar;
    
    [self addMySubViews];
    [self addMyContraints];
    [self initMenuView];
}

-(void)initMenuView{
    UIImage *image = [UIImage imageNamed:@"ic_filter_category_0"];
    __weak typeof(self) weakSelf = self;
    YCMenuAction *action = [YCMenuAction actionWithTitle:@"原图" image:image handler:^(YCMenuAction *action) {
        NSLog(@"点击了%@",action.title);
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.objRender setPictureFilter:PictureRenderFilter_NONE];
        [strongSelf.objRender setDisplay];
    }];
    YCMenuAction *actionGray =  [YCMenuAction actionWithTitle:@"灰度" image:image handler:^(YCMenuAction *action) {
        NSLog(@"点击了%@",action.title);
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.objRender setPictureFilter:PictureRenderFilter_GRAY];
        [strongSelf.objRender setDisplay];
    }];
    
    YCMenuAction *actionCool =  [YCMenuAction actionWithTitle:@"冷色" image:image handler:^(YCMenuAction *action) {
        NSLog(@"点击了%@",action.title);
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.objRender setPictureFilter:PictureRenderFilter_COOL];
        [strongSelf.objRender setDisplay];
    }];
    
    YCMenuAction *actionWarm =  [YCMenuAction actionWithTitle:@"暖色" image:image handler:^(YCMenuAction *action) {
        NSLog(@"点击了%@",action.title);
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.objRender setPictureFilter:PictureRenderFilter_WARM];
        [strongSelf.objRender setDisplay];
    }];
    
    YCMenuAction *actionBlur =  [YCMenuAction actionWithTitle:@"模糊" image:image handler:^(YCMenuAction *action) {
        NSLog(@"点击了%@",action.title);
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.objRender setPictureFilter:PictureRenderFilter_BLUR];
        [strongSelf.objRender setDisplay];
    }];
    
    YCMenuAction *actionMAGN =  [YCMenuAction actionWithTitle:@"放大" image:image handler:^(YCMenuAction *action) {
        NSLog(@"点击了%@",action.title);
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.objRender setPictureFilter:PictureRenderFilter_MAGN];
        [strongSelf.objRender setDisplay];
    }];
    
    self.arr = @[action,actionGray,actionCool,actionWarm,actionBlur,actionMAGN];
}

-(void)addMySubViews{
//    [self.view addSubview:self.menuBtn];
}
-(void)addMyContraints{
    self.menuBtn.frame = CGRectMake(ScreenWidth-100, 80, 60, 30);
}
//MARK:click
-(void)menuBtnClick:(UIButton *)btn{
    CGPoint P = CGPointMake(btn.frame.origin.x+btn.frame.size.width/2, btn.frame.origin.y+btn.frame.size.height/2);
    // 创建
    YCMenuView *view = [YCMenuView menuWithActions:self.arr width:140 atPoint:P];
    self.mView = view;
    
    // 自定义设置
    view.menuColor = [UIColor whiteColor];
    view.separatorColor = [UIColor whiteColor];
    view.maxDisplayCount = 20;
    view.offset = 100;
    view.textColor = [UIColor blueColor];
    view.textFont = [UIFont boldSystemFontOfSize:18];
    view.menuCellHeight = 50;
    view.dismissOnselected = YES;
    view.dismissOnTouchOutside = YES;

    // 显示
    [view show];
}
-(void)rightBar2Click:(UIBarButtonItem *)rightBar{
    NSLog(@"点击了菜单！");
    self.isProgressWhole = !self.isProgressWhole;
    NSString *barTitle = self.isProgressWhole ? @"处理全部":@"处理一半";
    rightBar.title = barTitle;
}

#pragma mark -- lazy load
- (UIButton *)menuBtn{
    if (_menuBtn == nil) {
        _menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _menuBtn.layer.borderWidth = 1;
        _menuBtn.layer.borderColor = [[UIColor orangeColor] CGColor];
        _menuBtn.layer.cornerRadius = 5;
        [_menuBtn setTitle:@"滤镜" forState:UIControlStateNormal];
        _menuBtn.titleLabel.textColor = [UIColor redColor];
        _menuBtn.backgroundColor = [UIColor greenColor];
        [_menuBtn addTarget:self action:@selector(menuBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _menuBtn;
}
@end
